﻿Imports TMySQL
Public Class frmAddColumns
    Private _Table As TMyTable
    Private _GridMyColumns As TGridMySQLColumn
    Private Enum TCol As Integer
        coColumnName = 0
        coColumnType = 1
        coDataLength = 2
        coCharacterSet = 3
        coDefault = 4
        coNullable = 5
        coCollation = 6
        coAttribute = 7
        coIndex = 8
        coAutoIncrement = 9
        coComment = 10
    End Enum

    Private Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
        frmMain.EditorSettings.ApplyAll(Me)
        frmMain.GeneralSettings.ApplyAll(Me)
        frmMain.GridSettings.ApplyAll(Me)
    End Sub
    Private ReadOnly Property ColumnTypes() As TMyColumnTypes
        Get
            Return frmMain.ColumnTypes
        End Get
    End Property
    Private ReadOnly Property Attributes() As TMyAttributes
        Get
            Return frmMain.Attributes
        End Get
    End Property
    Private ReadOnly Property Collations() As TMyCollations
        Get
            Return frmMain.Collations
        End Get
    End Property
    Private ReadOnly Property CharacterSets() As TMyCharacterSets
        Get
            Return frmMain.CharacterSets
        End Get
    End Property

    Public Sub New(ByVal ATable As TMyTable)
        Me.New()
        _GridMyColumns = New TGridMySQLColumn(GridCol, ATable, False)
        _Table = ATable
    End Sub

    Public Function ShowEx() As MsgBoxResult
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        InitFrm()
        Me.ShowDialog()
    End Function

    Private Sub InitFrm()
        Common.SetGridAllowSorting(GridCol, DataGridViewColumnSortMode.NotSortable)
        _GridMyColumns.InitColumns()
        InitCboColumns()
        InitCboAddPos()
        GridCol.Rows.Add()
    End Sub
    Private Sub InitCboColumns()
        For i As Integer = 0 To _Table.ColumnCount - 1
            cboColumn.Items.Add(_Table.Columns(i).ColumnName)
        Next
        cboColumn.SelectedIndex = 0
    End Sub
    Private Sub InitCboAddPos()
        cboAddPos.Items.Clear()
        cboAddPos.Items.Add("先頭")
        cboAddPos.Items.Add("末尾")
        cboAddPos.Items.Add("列の後")
        cboAddPos.SelectedIndex = 0
    End Sub

    Private Sub cmdAddRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddRow.Click
        GridCol.Rows.Add()
    End Sub

    Private Sub cmdDeleteRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDeleteRow.Click
        If GridCol.SelectedCells.Count <= 0 Then
            Return
        End If
        Dim row As Integer = GridCol.SelectedCells(0).RowIndex
        GridCol.Rows.RemoveAt(row)
    End Sub

    Private Sub cmdExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExecute.Click
        Try
            _GridMyColumns.ValidateEntries()
        Catch ex As Exception
            Call MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            Return
        End Try

        _GridMyColumns.Position = Me.AddPosition
        Dim SQL As String = _GridMyColumns.AlterTableAddSQL
        Try
            If Not Confirm(SQL) Then
                Return
            End If
            Call _GridMyColumns.AlterTableAdd()
            Call MsgBox("列を追加しました")
            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        Catch ex As Exception
            Call MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            Return
        End Try
    End Sub

    Private ReadOnly Property AddPosition() As String
        Get
            If cboAddPos.SelectedIndex = 0 Then
                Return "FIRST"
            ElseIf cboAddPos.SelectedIndex = 1 Then
                Return "LAST"
            Else
                Return cboColumn.SelectedItem.ToString
            End If
        End Get
    End Property

    Private Function Confirm(ByVal ASQL As String) As Boolean
        Dim frmRet As New frmConfirmMsg(ASQL)
        Try
            Return frmRet.ShowDialog = Windows.Forms.DialogResult.OK
        Finally
            frmRet.Dispose()
        End Try
    End Function
End Class